package tq.com.tqteacher.activity.newPublish;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.tqcom.base.BaseActivity;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.util.TQEventBus;
import tq.com.tqcom.util.TQLoadingHelper;
import tq.com.tqcom.util.TQSystemDefine;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.R;
import tq.com.tqteacher.model.TQNewPublish;
import tq.com.tqteacher.model.TQPublishType;
import tq.com.tqteacher.model.TQWKPublish;
import tq.com.tqteacher.net.TQNetInterface;
import tq.com.tqteacher.view.viewhold.TQPublishResultViewHelper;
import tq.com.tqteacher.view.viewhold.TQPublishSaveButtonViewHold;

public class TQWKResultActivity extends BaseActivity {
    private TQHeader5View mHeader5View;
    private TQPublishResultViewHelper mPublishResultViewHelper;
    private TQPublishSaveButtonViewHold mTQPublishSaveButtonViewHold;

    private TextView wk_count;
    private TextView wk_subtitle;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setStatusBar();
        setContentView(R.layout.activity_wkresult_layout);
        mHeader5View = findViewById(R.id.title);
        mHeader5View.getTitleView().setText("微课");
        mHeader5View.getBackIcon().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TQWKResultActivity.this.finish();
            }
        });

        wk_count = findViewById(R.id.wk_count);
        wk_subtitle = findViewById(R.id.subtitle);

        if(mPublishResultViewHelper == null){
            mPublishResultViewHelper = new TQPublishResultViewHelper(findViewById(R.id.baseInfo));
            mPublishResultViewHelper.setModifyClick(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoModify();
                }
            });
        }
        if(mTQPublishSaveButtonViewHold == null){
            mTQPublishSaveButtonViewHold = new TQPublishSaveButtonViewHold(findViewById(R.id.bottom));
            mTQPublishSaveButtonViewHold.setPublishClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onPublish();
                }
            });
        }
        findViewById(R.id.wk_area).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoWkList();
            }
        });

        if(!TQPublishType.getSharePublishItem().isModify()){
            gotoWkList();
        }
        if(TQPublishType.getSharePublishItem().getNeedloadRemote()){
//            getDetail();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        binData();
    }

    private void gotoModify(){
        TQPublishType.getSharePublishItem().setModify(true);
        Intent lIntent = new Intent(this, TQWKPublishActivity.class);
        startActivity(lIntent);
    }
    private void gotoWkList(){
        TQPublishType.getSharePublishItem().setModify(true);
        Intent lIntent = new Intent(this, TQWKListActivity.class);
        startActivity(lIntent);
    }
    private void getDetail(){
//        TQLoadingHelper.build(this).setCancelable(false);
//        TQNetInterface.getMatrialDetail(TQPublishType.getSharePublishItem().getSeq_id(), new INetCallback() {
//            @Override
//            public void result(TQNetResult pNetResult) {
//                if(!pNetResult.isSuccess()){
//                    TQLoadingHelper.setError(pNetResult.getErrMsg());
//                    return;
//                }
//
//                try {
//                    if(pNetResult.getData() == null)return;
//
//                    JSONObject lObject = pNetResult.getData().optJSONObject("material");
//                    if(lObject == null) return;
//
//                    JSONObject lBaseInfo = lObject.optJSONObject("material");
//                    if(lBaseInfo != null){
//                        TQPublishType.getSharePublishItem().fromJson(lBaseInfo);
//                    }
//
//                    JSONArray lAttarch = lObject.optJSONArray("attachment");
//                    TQPublishType.getSharePublishItem().refreshAttach(lAttarch);
//
//                    binData();
//                }finally {
//                    TQLoadingHelper.dimiss();
//                }
//            }
//        });

        TQPublishType.getSharePublishItem().getDetailFromServer(this, new TQNewPublish.INetFinishListener() {
            @Override
            public void finish() {
                binData();
            }
        });
    }
    private void onPublish(){
        if(TQPublishType.getTQOpenFrom() == TQPublishType.TQOpenFrom.optimize){
            AlertDialogHelper.show(this, "已同步更新");
        }else{
            TQPublishType.getSharePublishItem().onPublish(new INetCallback() {
                @Override
                public void result(TQNetResult pNetResult) {
                    if(!pNetResult.isSuccess()){
                        AlertDialogHelper.show(TQWKResultActivity.this, pNetResult.getErrMsg());
                        return;
                    }

                    TQPublishType.getSharePublishItem().setState(TQSystemDefine.PUBLISH_STATE_Publish);
                    mTQPublishSaveButtonViewHold.setAlreadyPublish();
                    TQEventBus.post(new TQEventBus.TQEventMessage(TQEventBus.TQEventMessage.Opertor.modify, null));
                    TQWKResultActivity.this.finish();
                }
            });
        }

    }

    private void binData(){
        if(TQPublishType.getSharePublishItem() instanceof TQWKPublish){
            mPublishResultViewHelper.setWkResult((TQWKPublish)(TQPublishType.getSharePublishItem()));
            TQWKPublish lTQWKPublish = (TQWKPublish)(TQPublishType.getSharePublishItem());
            wk_count.setText(String.format("微课已录制(%d)", lTQWKPublish.getVideoSetsModels().size()));

            wk_subtitle.setText(lTQWKPublish.getDesc());
        }
        if(TQPublishType.getSharePublishItem().isPublish()){
            mTQPublishSaveButtonViewHold.setAlreadyPublish();
        }

    }
}
